package ch.msoftch.gui.utils.popup.actions;

import org.eclipse.core.resources.*;
import org.eclipse.jface.action.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.*;
import ch.msoftch.gui.utils.*;

/**
 * Action used to create a property key class.
 * 
 * @author hautle
 */
public class CreateI18NKeysAction implements IObjectActionDelegate {
    /** The shell. */
    private Shell shell;

    /** The current selection. */
    private IStructuredSelection selection;

    /**
     * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
     */
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        shell = targetPart.getSite().getShell();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void run(IAction action) {
        final IFile file = (IFile) selection.getFirstElement();
        try {
            PropertyKeyClassBuilder.create(file);
        } catch (Exception e) {
            EclipseHelper.showError(shell, "Error while generating the class", e);
        }
    }

    /**
     * @see IActionDelegate#selectionChanged(IAction, ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
        this.selection = (IStructuredSelection) selection;
    }
}
